package org.ljy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 在其他类中调用
public class ZKCheckerDemo {
    public static void main(String[] args) {
        // 1. 创建工具类实例（可自定义超时时间）
        ZKNodeRoleChecker checker = new ZKNodeRoleChecker(3000, 3000);

        // 2. 定义节点列表
        String nodeList = "172.30.0.2:11181,172.30.0.3:11181,172.30.0.4:11181,172.30.0.5:11181,172.30.0.6:11181";

        // 获取IP->角色映射（ConcurrentHashMap）
        ConcurrentHashMap<String, String> ipToRole = checker.getIpToRoleMap(nodeList);

// 遍历映射结果
        for (Map.Entry<String, String> entry : ipToRole.entrySet()) {
            String ip = entry.getKey();
            String role = entry.getValue();
            System.out.println("IP: " + ip + ", 角色: " + role);
        }

        // 第一次调用：生成ServerRole-1.txt
        String file1 = checker.getAndSaveIpToRoleMap(nodeList);
        System.out.println("第一次更新文件：" + file1);

        // 模拟一段时间后再次更新（第二次调用：生成ServerRole-2.txt）
        try { Thread.sleep(2000); } catch (InterruptedException e) {}
        String file2 = checker.getAndSaveIpToRoleMap(nodeList);
        System.out.println("第二次更新文件：" + file2);

        String filePath = "ServerRole-1.txt";
        ConcurrentHashMap<String, String> roleMap1 = ZKNodeRoleChecker.readFromFile(filePath);
        System.out.println("从" + filePath + "读取的角色映射：");
        for (Map.Entry<String, String> entry : roleMap1.entrySet()) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}